using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for RoleControl.
	/// </summary>
	public class RoleControl : System.Windows.Forms.UserControl
	{
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label nameLabel;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.CheckedListBox usersListBox;
    private System.Windows.Forms.ListBox proceduresListBox;
    private System.Windows.Forms.Label label3;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public RoleControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Apply changes", "Apply any changes made", new ActionExecuteDelegate(ApplyChanges));
      actionList.Add("Refresh", "Refresh the data for this role", new ActionExecuteDelegate(RefreshRole));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RoleControl));
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.label1 = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.usersListBox = new System.Windows.Forms.CheckedListBox();
      this.proceduresListBox = new System.Windows.Forms.ListBox();
      this.label3 = new System.Windows.Forms.Label();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
      this.SuspendLayout();
      // 
      // pictureBox1
      // 
      this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(400, 8);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 40);
      this.pictureBox1.TabIndex = 0;
      this.pictureBox1.TabStop = false;
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(40, 16);
      this.label1.TabIndex = 1;
      this.label1.Text = "Name:";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.Location = new System.Drawing.Point(48, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.Size = new System.Drawing.Size(336, 16);
      this.nameLabel.TabIndex = 2;
      this.nameLabel.Text = "nameLabel";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 32);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(112, 16);
      this.label2.TabIndex = 3;
      this.label2.Text = "Users with this role:";
      // 
      // usersListBox
      // 
      this.usersListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                  | System.Windows.Forms.AnchorStyles.Left)));
      this.usersListBox.CheckOnClick = true;
      this.usersListBox.Location = new System.Drawing.Point(8, 48);
      this.usersListBox.Name = "usersListBox";
      this.usersListBox.Size = new System.Drawing.Size(192, 199);
      this.usersListBox.TabIndex = 4;
      this.usersListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.usersListBox_ItemCheck);
      // 
      // proceduresListBox
      // 
      this.proceduresListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                  | System.Windows.Forms.AnchorStyles.Left)));
      this.proceduresListBox.Location = new System.Drawing.Point(208, 48);
      this.proceduresListBox.Name = "proceduresListBox";
      this.proceduresListBox.Size = new System.Drawing.Size(144, 199);
      this.proceduresListBox.TabIndex = 6;
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(208, 32);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(128, 16);
      this.label3.TabIndex = 7;
      this.label3.Text = "Defined in procedures:";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(442, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 272);
      this.uiActionsControl.TabIndex = 8;
      // 
      // RoleControl
      // 
      this.Controls.Add(this.uiActionsControl);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.proceduresListBox);
      this.Controls.Add(this.usersListBox);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.nameLabel);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.pictureBox1);
      this.Name = "RoleControl";
      this.Size = new System.Drawing.Size(592, 272);
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private Role role;
    public void LoadRole(Role role)
    {
      this.role = role;
      nameLabel.Text = role.Name;

      // load all users
      UserCollection users = role.Server.Users;
      usersListBox.Items.Clear();
      for (int i = 0; i < users.Count; i++)
      {
        usersListBox.Items.Add(users[i].Name);
        if (role.IsHeldBy(users[i].Name))
          usersListBox.SetItemChecked(i, true);
      }

      proceduresListBox.Items.Clear();
      for (int i = 0; i < role.ProcedureCount; i++)
      {
        string procedure = role.Procedure(i);
        if (procedure != null)
          proceduresListBox.Items.Add(procedure);
      }
    }

    private string ApplyChanges()
    {
      using (new CursorHandler())
      {
        role.ApplyChanges();
        return Constants.ChangesApplied;
      }
    }

    private string RefreshRole()
    {
      using (new CursorHandler())
      {
        role.Refresh();
        LoadRole(role);
        return "";
      }
    }

    private void usersListBox_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
    {
      switch (e.NewValue)
      {
        case CheckState.Checked:
          role.GrantToUser(usersListBox.Items[e.Index].ToString());
          break;
        case CheckState.Unchecked:
          role.RevokeFromUser(usersListBox.Items[e.Index].ToString());
          break;
      } 
    }
	}
}
